home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / mega src / Source / ask.c next >
Encoding:
C/C++ Source or Header  |  1994-04-19  |  4.5 KB  |  197 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     ask.c
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #include "nshc.h"
  16.  
  17. #include "str_utl.proto.h"
  18. #include "nshc_utl.proto.h"
  19.  
  20. /* ======================================== */
  21.  
  22. // our data record, NewHandled and attached to nshc_parms->data
  23.  
  24. typedef struct {
  25.  
  26.     Str255    theString;        // theString we are receiving fromthe keyboard
  27.     int        overrun;        // non-zero if theString has exceeded 255 chars
  28.  
  29. } t_ask_data;
  30.  
  31. typedef t_ask_data **t_ask_handle;
  32.  
  33. /* ======================================== */
  34.  
  35. // prototypes - for local use only
  36.  
  37. int ask_by_parameters( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  38. void ask_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  39. void ask_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 theString );
  40. void ask_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  41. void ask_stop( t_nshc_parms *nshc_parms );
  42.  
  43. // utility
  44.  
  45. void ask_display( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, Str255 theString )
  46. {
  47.     int        size;
  48.  
  49.     size = 1;
  50.     
  51.     if (nshc_got_option( nshc_parms, 's')) size = 0;
  52.     if (nshc_got_option( nshc_parms, 'l')) size = 2;
  53.     
  54.     nshc_parms->result = nshc_calls->NSH_ask( theString, size );
  55. }
  56.  
  57. /* ======================================== */
  58.  
  59. // if the user put his text on the command line, do it all in one operation
  60.  
  61. int ask_by_parameters( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  62. {
  63.     int                arg;
  64.     int                got_one;
  65.     int                length;
  66.     char            *p;
  67.     Str255            theString;
  68.     
  69.     arg = 1;
  70.     got_one = 0;
  71.     length = 0;
  72.     theString[0] = 0;
  73.     
  74.     while ( arg < nshc_parms->argc ) {
  75.         if ( nshc_is_operand( nshc_parms, arg) ) {
  76.             p = &nshc_parms->arg_buf[nshc_parms->argv[arg]];
  77.             length += cStrLen(p);
  78.             if (length < 255) {
  79.                 if (got_one)
  80.                     theString[++theString[0]] = ' ';
  81.                 pStrAppendC( theString, p );
  82.                 }
  83.             got_one = 1;
  84.             }
  85.         arg++;
  86.         }
  87.         
  88.     if (got_one)
  89.         if (length <= 255)
  90.             ask_display( nshc_parms, nshc_calls, theString );
  91.         else{
  92.             nshc_calls->NSH_putStr_err("\pask: stdin exceeds 255 chars.\r");
  93.             nshc_parms->result = NSHC_ERR_GENERAL;
  94.             }
  95.  
  96.     return( got_one );
  97. }
  98.  
  99. /* ======================================== */
  100.  
  101. // state machine
  102.  
  103. void ask_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  104. {
  105.     t_ask_handle    ourData;
  106.  
  107.     nshc_parms->action = nsh_idle;
  108.     nshc_parms->result = NSHC_NO_ERR;
  109.     
  110.     if ( ask_by_parameters( nshc_parms, nshc_calls ) )
  111.         return;
  112.  
  113.     ourData = (t_ask_handle)NewHandle( sizeof(t_ask_data) );
  114.  
  115.     if (ourData) {
  116.         (**ourData).theString[0] = 0;
  117.         (**ourData).overrun = 0;
  118.         nshc_parms->data = (Handle)ourData;
  119.         nshc_parms->action = nsh_continue;
  120.         }
  121.     else {
  122.         nshc_calls->NSH_putStr_err("\pask: Could not allocate storage.\r");
  123.         nshc_parms->result = NSHC_ERR_MEMORY;
  124.         }
  125. }
  126.  
  127. /* ======================================== */
  128.  
  129. // this _continue routine is used to pick up input from stdin, it loops until
  130. // an end-of-input is found. It posts the input text if it does not exceed 255
  131. // characters, otherwise it posts an error message.  If input exceeds 255 chars,
  132. // all extra chars are eaten.
  133.  
  134. void ask_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  135. {
  136.     int        value;
  137.     int        size;
  138.     Str255    localString;
  139.     
  140.     t_ask_handle    ndata;
  141.     
  142.     ndata = (t_ask_handle)(nshc_parms->data);
  143.     
  144.     HLock( ndata );
  145.     
  146.     value = nshc_calls->NSH_getStr( localString );
  147.     
  148.     if (value) {
  149.         size = localString[0] + (**ndata).theString[0];
  150.         if (size > 255) (**ndata).overrun = 1;
  151.         if (!(**ndata).overrun) pStrAppend( (**ndata).theString, localString );
  152.         }
  153.         
  154.     if (value == -1) {
  155.     
  156.         if ((**ndata).overrun) {
  157.             nshc_calls->NSH_putStr_err("\pask: stdin exceeds 255 chars.\r");
  158.             nshc_parms->result = NSHC_ERR_GENERAL;
  159.             }
  160.         else
  161.             ask_display( nshc_parms, nshc_calls, (**ndata).theString );
  162.             
  163.         nshc_parms->action = nsh_stop;
  164.         }
  165.         
  166.     HUnlock( ndata );
  167. }
  168.  
  169. /* ======================================== */
  170.  
  171. void ask_stop( t_nshc_parms *nshc_parms )
  172. {        
  173.     if (nshc_parms->data)
  174.         DisposeHandle(nshc_parms->data);
  175.  
  176.     nshc_parms->action = nsh_idle;
  177. }
  178.  
  179. /* ======================================== */
  180.  
  181. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  182. {
  183.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) return;
  184.         
  185.     switch (nshc_parms->action) {
  186.         case nsh_start:
  187.             ask_start(nshc_parms,nshc_calls);
  188.             break;
  189.         case nsh_continue:
  190.             ask_continue(nshc_parms,nshc_calls);
  191.             break;
  192.         default:
  193.             ask_stop(nshc_parms);
  194.             break;
  195.         }
  196. }
  197.